﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using GatherWebLib.Model;
using System.Data;

namespace GatherWebLib
{
   public class cTask
    {
       cXmlIO xmlConfig;
       //加载一个任务到此类中
       public cTask_m LoadTask(String FileName)
       {
         return  LoadTaskInfo(FileName);
       }
     
       //加载指定任务的信息
       private cTask_m LoadTaskInfo(string FileName)
       {
           cTask_m task = new cTask_m();
           //根据一个任务名称装载一个任务
           try
           {
               xmlConfig = new cXmlIO(FileName);

               //获取TaskClass节点
               //TaskClass = xmlConfig.GetData("descendant::TaskClasses");
           }
           catch (System.Exception ex)
           {
               if (!File.Exists(FileName))
               {
                   throw new System.IO.IOException("您指定的任务文件不存在！");
               }
               else
               {
                   throw ex;
               }
           }

           //加载任务信息
           task.TaskID = Int64.Parse(xmlConfig.GetNodeValue("Task/BaseInfo/ID"));
           task.TaskName = xmlConfig.GetNodeValue("Task/BaseInfo/Name");

           ///加载任务版本信息，注意：1.0中是不存在版本信息描述的，所以如果加载1.0的任务
           ///会出错
           try
           {
               task.TaskVersion = Single.Parse(xmlConfig.GetNodeValue("Task/BaseInfo/Version"));
           }
           catch (System.Exception)
           {
               task.TaskVersion = Single.Parse("1.0");
           }




           task.TaskDemo = xmlConfig.GetNodeValue("Task/BaseInfo/TaskDemo");
           task.TaskClass = xmlConfig.GetNodeValue("Task/BaseInfo/Class");
           task.TaskType = xmlConfig.GetNodeValue("Task/BaseInfo/Type");
           task.RunType = xmlConfig.GetNodeValue("Task/BaseInfo/RunType");

           //因存的是相对路径，所以要加上系统路径
           task.SavePath = cXmlSConfig.TaskPath + xmlConfig.GetNodeValue("Task/BaseInfo/SavePath");
           task.UrlCount = int.Parse(xmlConfig.GetNodeValue("Task/BaseInfo/UrlCount").ToString());
           task.ThreadCount = int.Parse(xmlConfig.GetNodeValue("Task/BaseInfo/ThreadCount"));
           task.Cookie = xmlConfig.GetNodeValue("Task/BaseInfo/Cookie");
           task.DemoUrl = xmlConfig.GetNodeValue("Task/BaseInfo/DemoUrl");
           task.StartPos = xmlConfig.GetNodeValue("Task/BaseInfo/StartPos");
           task.EndPos = xmlConfig.GetNodeValue("Task/BaseInfo/EndPos");
           task.WebCode = xmlConfig.GetNodeValue("Task/BaseInfo/WebCode");
           task.IsLogin = (xmlConfig.GetNodeValue("Task/BaseInfo/IsLogin") == "True" ? true : false);
           task.LoginUrl = xmlConfig.GetNodeValue("Task/BaseInfo/LoginUrl");
           task.IsUrlEncode = (xmlConfig.GetNodeValue("Task/BaseInfo/IsUrlEncode") == "True" ? true : false);
           task.UrlEncode = xmlConfig.GetNodeValue("Task/BaseInfo/UrlEncode");

           task.ExportType = xmlConfig.GetNodeValue("Task/Result/ExportType");
           task.ExportFile = xmlConfig.GetNodeValue("Task/Result/ExportFileName");
           task.DataSource = xmlConfig.GetNodeValue("Task/Result/DataSource");
           task.DataTableName = xmlConfig.GetNodeValue("Task/Result/DataTableName");

           task.InsertSql = xmlConfig.GetNodeValue("Task/Result/InsertSql");
           task.ExportUrl = xmlConfig.GetNodeValue("Task/Result/ExportUrl");
           task.ExportUrlCode = xmlConfig.GetNodeValue("Task/Result/ExportUrlCode");
           task.ExportCookie = xmlConfig.GetNodeValue("Task/Result/ExportCookie");

           //加载高级配置信息
           task.GatherAgainNumber = int.Parse(xmlConfig.GetNodeValue("Task/Advance/GatherAgainNumber"));
           task.IsIgnore404 = (xmlConfig.GetNodeValue("Task/Advance/IsIgnore404") == "True" ? true : false);
           task.IsErrorLog = (xmlConfig.GetNodeValue("Task/Advance/IsErrorLog") == "True" ? true : false);
           task.IsDelRepRow = (xmlConfig.GetNodeValue("Task/Advance/IsDelRepeatRow") == "True" ? true : false);
           task.IsExportHeader = (xmlConfig.GetNodeValue("Task/Advance/IsExportHeader") == "True" ? true : false);
           task.IsTrigger = (xmlConfig.GetNodeValue("Task/Advance/IsTrigger") == "True" ? true : false);
           task.TriggerType = xmlConfig.GetNodeValue("Task/Advance/TriggerType");

           DataView dw = new DataView();
           int i;

           //加载Trigger信息
           dw = xmlConfig.GetData("descendant::Trigger");
           cTriggerTask_m tt;

           if (dw != null)
           {
               for (i = 0; i < dw.Count; i++)
               {
                   tt = new cTriggerTask_m();
                   tt.RunTaskType = int.Parse(dw[i].Row["RunTaskType"].ToString());
                   tt.RunTaskName = dw[i].Row["RunTaskName"].ToString();
                   tt.RunTaskPara = dw[i].Row["RunTaskPara"].ToString();

                   task.TriggerTask.Add(tt);
               }
           }

           dw = null;
           dw = new DataView();

           dw = xmlConfig.GetData("descendant::WebLinks");
           cWebLink_m w;

           DataView dn;

           if (dw != null)
           {
               task.WebpageLink = new List<cWebLink_m>();
               for (i = 0; i < dw.Count; i++)
               {
                   w = new cWebLink_m();
                   w.id = i;
                   w.Weblink = dw[i].Row["Url"].ToString();
                   if (dw[i].Row["IsNag"].ToString() == "True")
                       w.IsNavigation = true;
                   else
                       w.IsNavigation = false;

                   if (dw[i].Row["IsNextPage"].ToString() == "True")
                       w.IsNextpage = true;
                   else
                       w.IsNextpage = false;

                   w.NextPageRule = dw[i].Row["NextPageRule"].ToString();
                   w.IsGathered = int.Parse((dw[i].Row["IsGathered"].ToString() == null || dw[i].Row["IsGathered"].ToString() == "") ? "2031" : dw[i].Row["IsGathered"].ToString());

                   //加载导航规则
                   if (w.IsNavigation == true)
                   {
                       dn = dw[i].CreateChildView("WebLink_NavigationRules")[0].CreateChildView("NavigationRules_Rule");
                       cNavigRule_m nRule;

                       for (int m = 0; m < dn.Count; m++)
                       {
                           nRule = new cNavigRule_m();
                           nRule.Url = dn[m].Row["Url"].ToString();
                           nRule.Level = int.Parse(dn[m].Row["Level"].ToString());
                           nRule.NavigRule = dn[m].Row["NagRule"].ToString();

                           w.NavigRules.Add(nRule);
                       }
                   }
                   task.WebpageLink.Add(w);
                   w = null;
               }
           }

           dw = null;
           dw = new DataView();
           dw = xmlConfig.GetData("descendant::GatherRule");
           cWebpageCutFlag_m c;
           if (dw != null)
           {
               task.WebpageCutFlag = new List<cWebpageCutFlag_m>();
               for (i = 0; i < dw.Count; i++)
               {
                   c = new cWebpageCutFlag_m();
                   c.Title = dw[i].Row["Title"].ToString();
                   c.DataType = int.Parse((dw[i].Row["DataType"].ToString() == null || dw[i].Row["DataType"].ToString() == "") ? "0" : dw[i].Row["DataType"].ToString());
                   c.StartPos = dw[i].Row["StartFlag"].ToString();
                   c.EndPos = dw[i].Row["EndFlag"].ToString();
                   c.LimitSign = int.Parse((dw[i].Row["LimitSign"].ToString() == null || dw[i].Row["LimitSign"].ToString() == "") ? "0" : dw[i].Row["LimitSign"].ToString());

                   //处理版本不同时造成的错误，捕获不处理
                   try
                   {
                       c.RegionExpression = dw[i].Row["RegionExpression"].ToString();
                       c.ExportLimit = int.Parse((dw[i].Row["ExportLimit"].ToString() == null || dw[i].Row["ExportLimit"].ToString() == "") ? "0" : dw[i].Row["ExportLimit"].ToString());
                       c.ExportExpression = dw[i].Row["ExportExpression"].ToString();
                   }
                   catch (System.Exception)
                   {
                   }

                   task.WebpageCutFlag.Add(c);
                   c = null;
               }
           }
           dw = null;
          return task;
       }
    }
}
